var Workport = Workport || {};
// @Path("/Document")
Workport.DocumentAPI = (function(){
	
	var root = WP_RESTFUL_URL + (WP_RESTFUL_URL.endsWith('/') ? '' : '/') + 'Document/';
	
	var urls = ["Self", "MashUp", "{1}", "search", "{1}/Community", "{1}", "DiscardCheckOut/{1}", "{1}/Version",
	            "Copy", "Restore", "CopyTo" ];
	
	function getUrl(/*index, replace1, replace2, ...*/) {
		var index, i, loops, r;
		loops = arguments.length;
		if (loops > 0) {
			index = arguments[0];
			r = root + urls[index];
			for (i = 1; i < loops; i++)
				r = r.replace("{"+i+"}", arguments[i]);
		}
		return r;
	}
	
	return {
		// @GET		@Path("/Self")
		// public SelfDocumentResponse GetMyDocuments()
		getSelf:function(handler,request){
			var domain = {
				order:request.order,
				ps:request.pageSize != undefined?request.pageSize:10,
				filter:request.filter?request.filter:'Time',
				offset:request.offset?request.offset:"''"	
			};
			if(request.key){
				domain.key = request.key;
			}
			return sfw.Http.get(getUrl(0),function(xhr,data){
				if (!sfw.Http.handleError(xhr, data)) {
					if(jQuery.isFunction(handler)){
						handler(data);
					}
				}
			},domain);
			function toViewModel(data){
				return data;
			}
		},
		// @GET		@Path("/MashUp")
		// public CommunityDocumentResponse  GetAllDocuments()
		getAll:function(handler,request){
			return sfw.Http.get(getUrl(1),function(xhr,data){
				if (!sfw.Http.handleError(xhr, data)) {
					if(jQuery.isFunction(handler)){
						handler(data);
					}
				}
			},{
				order:request.order,
				ps:request.pageSize!= undefined?request.pageSize:10,
				sinceid:request.sinceid?request.sinceid:'0',
				filter:request.filter?request.filter:'Time',
				key:request.key?request.key:""
			});
			function toViewModel(data){
				return data;
			}
		},
		// @GET		@Path("/{DocumentId}")
		// public DocumentResponse GetDocumentDetails(@PathParam("DocumentId") String documentId)
		getDetail:function(handler,request){
			return sfw.Http.get(getUrl(2,request.documentId),function(xhr,data){
				if (!sfw.Http.handleError(xhr, data)) {
					if(jQuery.isFunction(handler)){
						handler(data);
					}
				}
			},{
				ntype:request.type,
				tid:request.targetId
			});
			function toViewModel(data){
				return data;
			}
		},
		// @GET		@Path("/search")
		// public SearchDocumentResponse  GetDocumentsSearch()
		search:function(handler,request){
			var domain = {
				key:request.key,
				entity:request.entity,
				ps:request.pageSize!= undefined?request.pageSize:10	
			};
			if(request.offset){
				domain.offset = request.offset;
			}
			return sfw.Http.get(getUrl(3),function(xhr,data){
				if (!sfw.Http.handleError(xhr, data)) {
					if(jQuery.isFunction(handler)){
						handler(data);
					}
				}
			},domain);
			function toViewModel(data){
				return data;
			}
		},
		// @GET		@Path("/{communityId}/Community")
		// public SelfDocumentResponse GetCommunityDocs(@PathParam("communityId") String communityId)
		getCommunity:function(handler,request){
			var domain = {
					ps:request.pageSize!= undefined?request.pageSize:10,
					offset:request.offset?request.offset:""
			};
			if(request.filter && request.order){
				domain.filter = request.filter;
				domain.order = request.order;
			}
			return sfw.Http.get(getUrl(4,request.communityId),function(xhr,data){
				if (!sfw.Http.handleError(xhr, data)) {
					if(jQuery.isFunction(handler)){
						handler(data);
					}
				}
			},domain );
			function toViewModel(data){
				return data;
			}
		},
		// @PUT		@Path("/{NotificationId}")
		// public BasicResponse CheckIn(@PathParam("NotificationId") String notificationId,CheckInRequest request)
		checkin:function(handler,request){
			return sfw.Http.put(getUrl(5,request.notificationId),function(xhr,data){
				if (!sfw.Http.handleError(xhr, data)) {
					if(jQuery.isFunction(handler)){
						handler(data);
					}
				}
			},{
				NotificationType : request.notificationType,
				DocumentId       : request.documentId,
				TargetId         : request.targetId,
				Version          : request.version,
				Comments		 : request.comments ? request.comments : "''",
				DocumentByte     : request.documentContent
			});
			function toViewModel(data){
				return data;
			}
		},
		// @POST		@Path("/{NotificationId}")
		// public BasicResponse CheckOut(@PathParam("NotificationId") String notificationId,CheckOutRequest request)
		checkout:function(handler,request){
			return sfw.Http.post(getUrl(5,request.notificationId),function(xhr,data){
				if (!sfw.Http.handleError(xhr, data)) {
					if(jQuery.isFunction(handler)){
						handler(data);
					}
				}
			},{
				NotificationType : request.notificationType,
				DocumentId       : request.documentId,
				TargetId         : request.targetId
			});
			function toViewModel(data){
				return data;
			}
		},
		// @POST		@Path("/DiscardCheckOut/{NotificationId}")
		// public BasicResponse DiscardCheckOut(@PathParam("NotificationId") String notificationId,CheckOutRequest request)
		discard:function(handler,request){
			return sfw.Http.post(getUrl(6,request.notificationId),function(xhr,data){
				if (!sfw.Http.handleError(xhr, data)) {
					if(jQuery.isFunction(handler)){
						handler(data);
					}
				}
			},{
				NotificationType : request.notificationType,
				DocumentId       : request.documentId,
				TargetId         : request.targetId
			});
			function toViewModel(data){
				return data;
			}
		},
		// @GET		@Path("/{DocumentId}/Version")
		// public DocumentVersionResponse GetVersionDetails(@PathParam("DocumentId") String documentId)
		getVersion:function(handler,request){
			return sfw.Http.get(getUrl(7,request.documentId),function(xhr,data){
				if (!sfw.Http.handleError(xhr, data)) {
					if(jQuery.isFunction(handler)){
						handler(data);
					}
				}
			},{
				ntype	: request.type,
				tid	 	: request.targetId
			});
			function toViewModel(data){
				return data;
			}
		},
		// @POST		@Path("/Copy")
		// public BasicResponse PostCopyDocument(DocumentCopyRequest request)
		copy:function(handler,request){
			return sfw.Http.post(getUrl(8),function(xhr,data){
				if (!sfw.Http.handleError(xhr, data)) {
					if(jQuery.isFunction(handler)){
						handler(data);
					}
				}
			},{
				TargetId		:	request.targetId,
				Tags			: 	request.tags ? request.tags : "''",
				EntityID		: 	request.entityId,
				EntityType		:	request.entityType,
				FromCommunityId	:	request.communityId,
				AttachContents	:	{
						AttachmentName  : request.attachmentName,
						AttachmentType  : request.attachmentType,
						Attachmentbytes : request.attachmentContent
					}
			});
			function toViewModel(data){
				return data;
			}
		},
		// @POST		@Path("/Restore")
		// public BasicResponse PostDocumentRestore(DocumentRestoreRequest request)
		restore:function(handler,request){
			return sfw.Http.post(getUrl(9),function(xhr,data){
				if (!sfw.Http.handleError(xhr, data)) {
					if(jQuery.isFunction(handler)){
						handler(data);
					}
				}
			},{
				DocumentId : request.documentId,
				VersionNo  : request.version,
				ntype      : request.type,
				tid        : request.targetId
			});
			function toViewModel(data){
				return data;
			}
		},
		// @POST		@Path("/CopyTo")
		// public BasicResponse PostCopyDocument(DocumentCopyRequest request)
		copyto:function(handler,request){
			return sfw.Http.post(getUrl(10),function(xhr,data){
				if (!sfw.Http.handleError(xhr, data)) {
					if(jQuery.isFunction(handler)){
						handler(data);
					}
				}
			},{},{
				SourceId		:	request.src,
				DestinationId	: 	request.dest,
				DocumentId		: 	request.documentId,
				NotificationType:	request.type
			});
			function toViewModel(data){
				return data;
			}
		}
	}
})();
